import json

import requests
import pytest

from testData.all_sites_user_info import all_sites_user_info, base_url

campaign_id = ''


# @pytest.mark.parametrize('params', all_sites_user_info)
# @pytest.mark.run(order=0)
# 这种方式在agent_console获取不了campaign_id
def no_test_create_new_campaign(params):
    r = ''
    try:
        # 1. 先登陆，获得token
        headers2 = {"Content-Type": r"application/json"

                    }
        url = 'https://autoportal.comm100dev.io/api/global/jwttoken/login?siteid=%s' % params["site_id"]
        data = {
            "IsAutoLogin": False,
            "email": params["email"],
            "password": params["password"],
            "siteId": params["site_id"]
        }
        r_login = requests.post(url, headers=headers2, data=json.dumps(data))
        r = r_login.json()['jwtToken']
    except Exception as e:
        print("获取token失败", e)

    try:
        # 创建新的campaign
        headers3 = {'Authorization': 'Bearer {access_token}'.format(access_token=r),
                    'Content-Type': r'application/json'
                    }
        campaign_data = {
            'name': 'testAutomationTest',
            'language': 'english'
        }
        creat_campaign_url = r'https://autoapi.comm100dev.io/v4/livechat/campaigns'
        r_create_campaign = requests.post(creat_campaign_url, headers=headers3, data=json.dumps(campaign_data))
        campaign_id = json.loads(r_create_campaign.text)["id"]
        print("campaign_id==>", campaign_id)
    except Exception as e:
        print("创建cmpaign失败", e)


@pytest.mark.parametrize('params', all_sites_user_info)
@pytest.mark.run(order=46)
def test_delete_campaign_and_auto_invitation(params):
    try:
        # 1. 先登陆，获得token
        headers = {"Content-Type": r"application/json"
                   }
        url = 'https://autoportal.comm100dev.io/api/global/jwttoken/login?siteid=%s' % params["site_id"]
        data = {
            "IsAutoLogin": False,
            "email": params["email"],
            "password": params["password"],
            "siteId": params["site_id"]
        }
        r_login = requests.post(url, headers=headers, data=json.dumps(data))
        r = r_login.json()['jwtToken']

        # 查出所有campaign
        headers1 = {'Authorization': 'Bearer {access_token}'.format(access_token=r)}
        campaign_list = requests.get(r'https://autoapi.comm100dev.io/v4/livechat/campaigns', headers=headers1)

        campaign_json = json.loads(campaign_list.text)

        # 2. 根据ID删除campaign
        for campaign in campaign_json:
            if campaign["name"] == 'testAutomationTest':
                del_url = r'https://autoapi.comm100dev.io/v4/livechat/campaigns/' + campaign["id"]
                del_campaign = requests.delete(del_url, headers=headers1)
                break

        # 3.删除auto invitations
        # 3.1 获取所有的invitations
        get_auto_invitations_url = r'https://autoapi.comm100dev.io/v4/livechat/autoInvitations'
        auto_invitations = requests.get(get_auto_invitations_url, headers=headers1)
        print("auto_invitations===>", auto_invitations)
        auto_invitations_json = json.loads(auto_invitations.text)

        for invitation in auto_invitations_json:
            if invitation["name"] == "test":
                del_auto_invitations_url = r'https://autoapi.comm100dev.io/v4/livechat/autoInvitations/' + invitation[
                    "id"]
                r = requests.delete(del_auto_invitations_url, headers=headers1)
                break

        # 4. 删除conversion
        # 4.1 获取所有conversions
        get_conversion_url = r'https://autoapi.comm100dev.io/v4/livechat/conversionActions'
        conversions = requests.get(get_conversion_url, headers=headers1)
        print("conversions==>",conversions)
        conversions_json = json.loads(conversions.text)
        for conversion in conversions_json['conversionActions']:
            if conversion['name'] == 'autotest':
                del_conversion_url = r'https://autoapi.comm100dev.io/v4/livechat/conversionActions/' + conversion["id"]
                r = requests.delete(del_conversion_url, headers=headers1)

    except Exception as e:
        print("删除campaign失败", e)


